package com.example.smartcity.ui.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smartcity.R;
import com.example.smartcity.ui.Bean.TopicBean;


import java.util.ArrayList;

public class TopicAdapter extends BaseAdapter {
    ArrayList<TopicBean> list;
    int Resource;
    public TopicAdapter(ArrayList<TopicBean> list, int Resource){
        this.list = list;
        this.Resource = Resource;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        ViewHolder viewHolder =null;
        if (convertView==null){
            view = LayoutInflater.from(parent.getContext()).inflate(Resource,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.topicImage = view.findViewById(R.id.topicImage);
            viewHolder.topicMessage = view.findViewById(R.id.topicText);
            view.setTag(viewHolder);
        }else{
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        Glide.with(parent.getContext()).load(list.get(position).getUrl()).into(viewHolder.topicImage);
        viewHolder.topicMessage.setText(list.get(position).getMessage());
        return view;
    }
    class ViewHolder{
        ImageView topicImage;
        TextView topicMessage;
    }
}
